let _debounceTimeout = null,
	_throttleRunning = false

/**
 * 防抖
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms   
 */
export const debounce = (fn, delay = 1000) => {
	clearTimeout(_debounceTimeout);
	_debounceTimeout = setTimeout(() => {
		fn();
	}, delay);
}
/**
 * 节流
 * @param {Function} 执行函数
 * @param {Number} delay 延时ms  
 */
export const throttle = (fn, delay = 1000) => {
	if (_throttleRunning) {
		return;
	}
	_throttleRunning = true;
	fn();
	setTimeout(() => {
		_throttleRunning = false;
	}, delay);
}
/**
 * toast
 */
export const msg = (title = '', param = {}) => {
	if (!title) return;
	uni.showToast({
		title,
		duration: param.duration || 1500,
		mask: param.mask || false,
		icon: param.icon || 'none'
	});
}
/**
 * showModel
 * @param {content} 提示内容
 *  * @param {showCancel} 是否显示取消
 */
export const showModel = (fun, content, showCancel = false) => {
	uni.showModal({
		showCancel,
		content,
		success(res) {
			if (res.confirm) fun();
		}
	})
}
//  setNav
export const setNav = (title = '') => {
	if (!title) return;
	uni.setNavigationBarTitle({
		title
	})
}
/**
 * 检查登录
 * @return {Boolean}
 */
export const isLogin = (options = {}) => {
	const token = uni.getStorageSync('uniIdToken');
	if (token) {
		return true;
	}
	if (options.nav !== false) {
		uni.navigateTo({
			url: '/pages/auth/login'
		})
	}
	return false;
}
/**
 * 数字 格式化
 * @param {Number} number  数字 格式化12,345.00
 * @return {Object} 
 */

export const formatter = (number)=> {
	let num = typeof(number) == 'number' ? number.toString().split('.')[0] : number.split('.')[0];
	let fixed=typeof(number) == 'number' ? number.toString().split('.')[1] : number.split('.')[1];
	const numbers = num.split('').reverse()
	const segs = []
	while (numbers.length) segs.push(numbers.splice(0, 3).join(''))
	return  fixed?segs.join(',').split('').reverse().join('')+'.'+fixed:segs.join(',').split('').reverse().join('')+'.00'
}
/**
 * 获取页面栈
 * @param {Number} preIndex为1时获取上一页
 * @return {Object} 
 */
export const prePage = (preIndex = 1) => {
	const pages = getCurrentPages();
	const prePage = pages[pages.length - (preIndex + 1)];
	return prePage.$vm;
}
/**
 * 格式化时间戳 Y-m-d H:i:s
 * @param {String} format Y-m-d H:i:s
 * @param {Number} timestamp 时间戳   
 * @return {String}
 */
export const date = (format, timeStamp) => {
	let _date = new Date(timeStamp),
		Y = _date.getFullYear(),
		m = _date.getMonth() + 1,
		d = _date.getDate(),
		H = _date.getHours(),
		i = _date.getMinutes(),
		s = _date.getSeconds();

	m = m < 10 ? '0' + m : m;
	d = d < 10 ? '0' + d : d;
	H = H < 10 ? '0' + H : H;
	i = i < 10 ? '0' + i : i;
	s = s < 10 ? '0' + s : s;

	return format.replace(/[YmdHis]/g, key => {
		return {
			Y,
			m,
			d,
			H,
			i,
			s
		} [key];
	});
}
export const messageDate = (timeStamp) => {
	if ('' + timeStamp.length <= 10) {
		timeStamp = +timeStamp * 1000;
	} else {
		timeStamp = +timeStamp;
	}
	let _date = new Date(timeStamp),
		Y = _date.getFullYear(),
		m = _date.getMonth() + 1,
		d = _date.getDate(),
		H = _date.getHours(),
		i = _date.getMinutes(),
		s = _date.getSeconds();
	let vTime = (new Date().getTime() - _date) / 1000,
		time = '';
	let vd = new Date().getDate(),
		vm = new Date().getMonth() + 1;
	i = i < 10 ? '0' + i : i;
	if (vTime < 180) {
		time = '刚刚'
	} else if (vTime >= 180 && vd == d && vm == m) {
		time = H + ':' + i
	} else {
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		time = Y + '-' + m + '-' + d
	}
	return time;
}
//二维数组去重
export const getUnique = array => {
	let obj = {}
	return array.filter((item, index) => {
		let newItem = item + JSON.stringify(item)
		return obj.hasOwnProperty(newItem) ? false : obj[newItem] = true
	})
}
//格式化  富文本
export const fromContent = (str) => {
	let s = "";
	let width = uni.getSystemInfoSync().screenWidth;
	s = str.replace(/&amp;/g, "&");
	s = s.replace(/&lt;/g, "<");
	s = s.replace(/&gt;/g, ">");
	s = s.replace(/&nbsp;/g, " ");
	s = s.replace(/&#39;/g, "'");
	s = s.replace(/&quot;/g, '"');
	s = s.replace(/<br\/>/g, "\n");
	s = s.replace(/style="[^"]+"/gi, '');
	s = s.replace(/width="[^"]+"/gi, '');
	s = s.replace(/height="[^"]+"/gi, '');
	s = s.replace(/\<img/gi, '<img style=width:' + width + "px;height:auto;display:block;");
	return s;
}
// 判断类型集合
export const checkStr = (str, type) => {
	switch (type) {
		case 'mobile': //手机号码
			return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
		case 'tel': //座机
			return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
		case 'card': //身份证
			return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
		case 'mobileCode': //6位数字验证码
			return /^[0-9]{6}$/.test(str)
		case 'pwd': //密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线
			return /^([a-zA-Z0-9_]){6,18}$/.test(str)
		case 'payPwd': //支付密码 6位纯数字
			return /^[0-9]{6}$/.test(str)
		case 'postal': //邮政编码
			return /[1-9]\d{5}(?!\d)/.test(str);
		case 'QQ': //QQ号
			return /^[1-9][0-9]{4,9}$/.test(str);
		case 'email': //邮箱
			return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
		case 'money': //金额(小数点2位)
			return /^\d*(?:\.\d{0,2})?$/.test(str);
		case 'URL': //网址
			return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
		case 'IP': //IP
			return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
		case 'date': //日期时间
			return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) ||
				/^(\d{4})\-(\d{2})\-(\d{2})$/.test(str)
		case 'number': //数字
			return /^[0-9]$/.test(str);
		case 'english': //英文
			return /^[a-zA-Z]+$/.test(str);
		case 'chinese': //中文
			return /^[\\u4E00-\\u9FA5]+$/.test(str);
		case 'lower': //小写
			return /^[a-z]+$/.test(str);
		case 'upper': //大写
			return /^[A-Z]+$/.test(str);
		case 'HTML': //HTML标记
			return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
		case "brank": //  银行卡号
			return /^([1-9]{1})(\d{14}|\d{18})$/.test(str.replace(/\s+/g, ""));
		default:
			return true;
	}
}
export const formatTime = (dateStr) => {
	var UNITS = {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	};
	var date = new Date(dateStr)
	var diff = Date.now() - date.getTime();
	if (diff < UNITS['天']) {
		var humanize = '';
		for (var key in UNITS) {
			if (diff >= UNITS[key]) {
				humanize = Math.floor(diff / UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	} else {
		return dateStr
	}
};
